package com.test.controller;

import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.test.common.util.KeyGenerator;
import com.test.model.User;

public class UserController extends Controller{
	
	/**
	 * 跳转到注册页面
	 *<p>name:guanyuhui 
	 *<p>Title: register</p>
	 *<p>Description:TODO</p>
	 * @param  设定文件
	 * @return  void 返回类型
	 * @throws
	 */
	public void register(){
		render("/user_reg.jsp");
	}
	
	/**
	 * 登录成功跳转
	 *<p>name:guanyuhui 
	 *<p>Title: login</p>
	 *<p>Description:TODO</p>
	 * @param  设定文件
	 * @return  void 返回类型
	 * @throws
	 */
	public void login(){
		String username= getPara("username");
		String password= getPara("password");
		String sql = "select * from user where username='"+username+"' and password = '"+password+"'";
		List<User> list = User.dao.find(sql);
		if(list != null && list.size() > 0){
			setSessionAttr("user", list.get(0));
			forwardAction("/article/list");
		}else{
			render("/home_common.jsp");
		}
	}
	
	public void save(){
		String id= KeyGenerator.getUUID();
		String username= getPara("username");
		String realname= getPara("realname");
		String password= getPara("password");
		User.dao.set("id",id).set("username", username).set("password", password).set("realname", realname).set("reg_date",new Date()).set("last_logdate", new Date()).save();
		User user = User.dao.findById(id);
		setSessionAttr("user", user);
		render("/article_common.jsp");
	}
	
	public void logout(){
		removeSessionAttr("user");
		render("/home_common.jsp");
	}
}
